import java.util.Stack;

/**
 * @ClassName LC1047
 * @Description TODO
 * @Author ZJX
 * @Date 2024/9/19 8:38
 * @Version 1.0
 */
public class LC1047 {
    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            char ch  = s.charAt(i);
            if (!stack.isEmpty() && stack.peek().equals(ch)){
                stack.pop();
            } else {
                stack.push(ch);
            }
        }

        // 将栈中字符拼接成字符串
        StringBuilder result = new StringBuilder();
        for (char c : stack) {
            result.append(c);
        }

        return result.toString();
    }
}
